package com.cn.lyl.dao.impl;

import com.cn.lyl.dao.LeavingDao;
import com.cn.lyl.model.LeavingModel;
import com.cn.lyl.util.JDBC;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LeavingDaoImpl implements LeavingDao {
    @Override
    public List<LeavingModel> findAll(LeavingModel leavingModel) {
        String sql ="select id ,mail, name ,phone,content,DATE_FORMAT(create_time,'%Y-%m-%d')as create_time from leaving  where 1=1 ";
        String stringSql = commonSql(leavingModel);
        int pageCurr=leavingModel.getPage().getPageCurr();
        int pageSize=leavingModel.getPage().getPageSize();
        stringSql +=" order by id desc limit "+(pageCurr-1)*pageSize+","+pageSize;
        sql = sql + stringSql;
        System.out.println(sql);
        return setList(sql);
    }

    @Override
    public LeavingModel findById(int id) {
        String sql ="select id , mail,name ,phone,content,DATE_FORMAT(create_time,'%Y-%m-%d')as create_time from leaving  where id= "+id;
        return setList(sql).size()>0?setList(sql).get(0):null;
    }

    @Override
    public int delete(int id) {
        String sql="delete from leaving where id ="+id;
        return JDBC.update(sql);
    }

    private List<LeavingModel> setList(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<String> colList = new ArrayList<>();
        try {
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 1; i <=metaData.getColumnCount(); i++) {

                colList.add(metaData.getColumnLabel(i));

            }
        } catch (SQLException e) {
            return null;
        }
        List<LeavingModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                LeavingModel model = new LeavingModel();
                if (colList.contains("id")) {
                    model.setId(resultSet.getInt("id"));
                }
                if (colList.contains("name")) {
                    model.setName(resultSet.getString("name"));
                }
                if (colList.contains("phone")) {
                    model.setPhone(resultSet.getString("phone"));
                }
                if (colList.contains("mail")) {
                    model.setMail(resultSet.getString("mail"));
                }
                if (colList.contains("content")) {
                    model.setContent(resultSet.getString("content"));
                }
                if (colList.contains("create_time")) {
                    model.setCreateTime(resultSet.getString("create_time"));
                }
                if (colList.contains("update_time")) {
                    model.setUpdateTime(resultSet.getString("update_time"));
                }
                if (colList.contains("enable")) {
                    model.setEnable(resultSet.getInt("enable"));
                }

                if (colList.contains("remark")) {
                    model.setRemark(resultSet.getString("remark"));
                }


                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    private String commonSql(LeavingModel leavingModel) {
        StringBuffer stringBuffer = new StringBuffer();
        if (leavingModel.getName() != null && !"".equals(leavingModel.getName())) {
            stringBuffer.append(" and name like'%" +leavingModel.getName() + "%'");
        }
        if (leavingModel.getPhone() != null && !"".equals(leavingModel.getPhone())) {
            stringBuffer.append(" and phone like'%" +leavingModel.getPhone() + "%'");
        }
        if (leavingModel.getMail() != null && !"".equals(leavingModel.getMail())) {
            stringBuffer.append(" and mail like'%" +leavingModel.getMail() + "%'");
        }
        if (leavingModel.getContent() != null && !"".equals(leavingModel.getContent())) {
            stringBuffer.append(" and content like'%" +leavingModel.getContent() + "%'");
        }
        if ((leavingModel.getStart() != null && !"".equals(leavingModel.getStart())) && (leavingModel.getEnd() == null || "".equals(leavingModel.getEnd()))) {
            stringBuffer.append(" and create_time >='" + leavingModel.getStart() + "'");
        }
        if ((leavingModel.getStart() == null || "".equals(leavingModel.getStart())) && (leavingModel.getEnd() != null && !"".equals(leavingModel.getEnd()))) {
            stringBuffer.append(" and create_time <='" + leavingModel.getEnd() + "'");
        }
        if ((leavingModel.getStart() != null || !"".equals(leavingModel.getStart())) && (leavingModel.getEnd() != null && !"".equals(leavingModel.getEnd()))) {
            stringBuffer.append(" and create_time between'" + leavingModel.getStart() + "' and '" + leavingModel.getEnd() + "'");
        }
        return stringBuffer.toString();
    }
}
